package com.example.snowli01;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class EditActivity extends AppCompatActivity {

    private EditText word;
    private EditText tran;
    private Button btn_update;
    private Button btn_delete;

    //修改单词的id
    private int id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        //弹窗对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(R.layout.dialog_layout);
        //创建弹窗对象
        AlertDialog dialog = builder.create();


        Intent intent = getIntent();
        String wordAndTran = intent.getStringExtra("wordAndT");
//        int id = intent.getIntExtra("id",0);
        //根据wordAndT分割，获取id
        int id = Integer.parseInt(wordAndTran.split("[:]")[0]);




        System.out.println(id);

        //绑定控件
        getviews();


        //分割字符串
        String wordStr = wordAndTran.split(":")[1];
        String tranStr = wordAndTran.split(":")[2];

        //将字符串存到相应位置
        word.setText(wordStr);
        tran.setText(tranStr);






        //修改单词
        //获取输入框的值
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String insertWord = word.getText().toString();
                String translation = tran.getText().toString();
                ContentValues cValue = new ContentValues();
                cValue.put("word",insertWord);
                cValue.put("tran",translation);
                String whereClause = "id = ?";
                String[] whereArgs = {String.valueOf(id)};

                //存入数据库
                com.example.snowli01.MainActivity.db.update("words",cValue,whereClause,whereArgs);
                //弹窗 插入成功
                //弹窗提示 购买成功
                //设置弹窗样式
                dialog.setMessage("更新成功");
                dialog.getWindow().getAttributes().windowAnimations = androidx.appcompat.R.style.AlertDialog_AppCompat;
                dialog.getWindow().setGravity(Gravity.CENTER);

                //显示弹窗
                dialog.show();




            }
        });



        //删除单词
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String whereCluse = "id = ?";
                String[] whereArgs = {String.valueOf(id)};
                com.example.snowli01.MainActivity.db.delete("words",whereCluse,whereArgs);
                word.setText("");
                tran.setText("");

                //提示删除成功
                dialog.setMessage("删除成功");
                dialog.getWindow().getAttributes().windowAnimations = androidx.appcompat.R.style.AlertDialog_AppCompat;
                dialog.getWindow().setGravity(Gravity.CENTER);

                //显示弹窗
                dialog.show();



            }
        });




    }



    private void getviews() {
        word = findViewById(R.id.word);
        tran = findViewById(R.id.tran);
        btn_update = findViewById(R.id.btn_update);
        btn_delete = findViewById(R.id.btn_delete);
    }





}








